#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void * start_routine(void * arg)
{
    printf(">>>进入子线程 \n");

    printf("子线程ID： %ld \n" , pthread_self());

    printf("子线程：%ld 即将退出 \n" , pthread_self());
    
    printf("退出子线程<<< \n");
    sleep(2);

    pthread_exit((void *)pthread_self());
}

int main()
{
    // 1. 创建子线程
    pthread_t pthid;
    pthread_t pthid2;
    pthread_t pthid3;
    pthread_create(&pthid , NULL , start_routine , NULL);
    pthread_create(&pthid2 , NULL , start_routine , NULL);
    pthread_create(&pthid3 , NULL , start_routine , NULL);

    // 等待子线程结束并获取退出状态
    void *status;
    pthread_join(pthid , &status);
    printf("\n主线程获取子线程 %ld 的退出状态：%ld\n", pthid, (long)status);
                    
    pthread_join(pthid2 , &status);
    printf("主线程获取子线程 %ld 的退出状态：%ld\n", pthid2, (long)status);
                                
    pthread_join(pthid3 , &status);
    printf("主线程获取子线程 %ld 的退出状态：%ld\n", pthid3, (long)status);
    
    return 0;
}

